<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增关于我们')" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-content-add">
        <input name="kind" class="form-control" type="hidden" value="ABOUT">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">标题：</label>
            <div class="col-sm-8">
                <textarea name="title" class="form-control" placeholder="关于我们标题"></textarea>
            </div>
        </div>


        <div class="form-group">
            <label class="col-sm-3 control-label">内容：</label>
            <div class="col-sm-8">
                <input type="hidden" class="form-control" name="content">
                <div class="summernote" id="content"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">排序：</label>
            <div class="col-sm-8">
                <input name="idx" class="form-control" type="number">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">跳转地址：</label>
            <div class="col-sm-8">
                <input name="ext2" class="form-control" type="text" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">预览图</label>
            <input type="hidden" name="ext1" id="ext1"/>
            <div class="col-sm-8">
                <input id="singleFileExt" name="file" type="file">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">背景图</label>
            <input type="hidden" name="thumb" id="thumb"/>
            <div class="col-sm-8">
                <input id="singleFile" name="file" type="file">
            </div>
        </div>
    </form>

    <div class="box-footer">
        <div class="col-sm-offset-5 col-sm-6">
            <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
</div>


<th:block th:include="include :: footer" />
<th:block th:include="include :: summernote-js" />
<th:block th:include="include :: bootstrap-fileinput-js" />
<script th:inline="javascript">
    var prefix = ctx + "news/content"
    $("#form-content-add").validate({
        rules: {
            title: "required",
            idx: "required"
        },
        messages: {
            title:  "必填项",
            idx:  "必填项"
        }
    });

    $('#singleFileExt').fileinput({
        uploadUrl: ctx + 'common/upload',
        maxFileCount: 1,
        autoReplace: true,
        initialPreviewAsData: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        var rsp = data.response;
        log.info("return url：" + rsp.url)

        $('#ext1').val(rsp.fileName)
    }).on('fileremoved', function (event, id, index) {
        $('#ext1').val('')
        $("input[name='" + event.currentTarget.id + "']").val('')
    }).on('filedeleted', function() {
        $('#ext1').val('')
    }).on('filesuccessremove', function() {
        $('#ext1').val('')
    }).on('filecleared', function() {
        $('#ext1').val('')
    })


    $("#singleFile").fileinput({
        uploadUrl: ctx + 'common/upload',
        maxFileCount: 1,
        autoReplace: true,
        initialPreviewAsData: true
    }).on('fileuploaded', function (event, data, previewId, index) {
        var rsp = data.response;
        log.info("return url：" + rsp.url)

        $('#thumb').val(rsp.fileName)
    }).on('fileremoved', function (event, id, index) {
        $('#thumb').val('')
        $("input[name='" + event.currentTarget.id + "']").val('')
    }).on('filedeleted', function() {
        $('#thumb').val('')
    }).on('filesuccessremove', function() {
        $('#thumb').val('')
    }).on('filecleared', function() {
        $('#thumb').val('')
    })

    function submitHandler() {
        if ($.validate.form()) {
            $.operate.saveTab(prefix + "/add", $('#form-content-add').serialize());
        }
    }

    $(function() {
        $('.summernote').summernote({
            lang: 'zh-CN',
            dialogsInBody: true,
            height: 400,
            callbacks: {
                onChange: function(contents, $edittable) {
                    $("input[name='" + this.id + "']").val(contents);
                },
                onImageUpload: function(files) {
                    var obj = this;
                    var data = new FormData();
                    data.append("file", files[0]);
                    $.ajax({
                        type: "post",
                        url: ctx + "common/upload",
                        data: data,
                        cache: false,
                        contentType: false,
                        processData: false,
                        dataType: 'json',
                        success: function(result) {
                            if (result.code == web_status.SUCCESS) {
                                $('#' + obj.id).summernote('insertImage', result.url);
                            } else {
                                $.modal.alertError(result.msg);
                            }
                        },
                        error: function(error) {
                            $.modal.alertWarning("图片上传失败。");
                        }
                    });
                }
            }
        });
    });
</script>
</body>
</html>